package Server.Poker.DGT;

public abstract class PlayerBase extends TimerCallBack {
	static int cntPlayer = 0;

	PlayerBase() {
		mTimerManager.SetCallBack(this);

		// m_pRecvBuffer = new RecvBuffer();
		// m_pRecvBuffer.Create(20480, 4096);
		cntPlayer++;
	}

	void SetChairID(int chair) {
		mChairID = chair;
	}

	int GetChairID() {
		return mChairID;
	}

	// ��ȡ״̬
	int GetGameStatus() {
		return m_cbGameStatus;
	}

	// ����״̬
	void SetGameStatus(int cbGameStatus) {
		m_cbGameStatus = cbGameStatus;
	}

	int m_cbGameStatus; // ��Ϸ״̬

	// RecvBuffer m_pRecvBuffer;
	int mChairID;
	TimerManager mTimerManager = new TimerManager();
	DGTZTable mTable = null;

	void SetTable(DGTZTable tb) {
		mTable = tb;
	}

	boolean OnGameMessage(int wSubCmdID, Object pMsg, int wSize) {
		MSG_FOR_TABLE pTableCmd = (MSG_FOR_TABLE) pMsg;
		Object pData = pTableCmd.byData;
		// printf("OnGameMessage:%d\n",wSubCmdID);
		// GAME_LOG( "<%s>:\t %s\t\t chair:%d\n",
		// GetChairID()==USER_INDEX?"UserPlayer":"AIPlayer",
		// GetMsgStr(wSubCmdID), GetChairID() );
		int wDataSize = 0;
		switch (wSubCmdID) {
		case Constants.SUB_S_GAME_SCENE:// ��Ϸ����
		{
			return OnEventGameScene(pTableCmd.m_wSubMsgType, false, pTableCmd, 0);
		}
		case Constants.SUB_S_SEND_CARD: // ��Ϸ��ʼ
		{
			return OnSubSendCard(pData, wDataSize);
		}
		case Constants.SUB_S_LAND_SCORE: // �û��з�
		{
			return OnSubLandScore(pData, wDataSize);
		}
		case Constants.SUB_S_GAME_START: // ׯ����Ϣ
		{
			return OnSubGameStart(pData, wDataSize);
		}
		case Constants.SUB_S_OUT_CARD: // �û�����
		{
			return OnSubOutCard(pData, wDataSize);
		}
		case Constants.SUB_S_PASS_CARD: // �û�����
		{
			return OnSubPassCard(pData, wDataSize);
		}
		case Constants.SUB_S_GAME_END: // ��Ϸ����
		{
			return OnSubGameEnd(pData, wDataSize);
		}
		case Constants.SUB_C_TRUSTEE: // ����й�
		{
			return OnSubTrustee(pData, wDataSize);
		}
		case Constants.SUB_GR_USER_INFO:
		case Constants.SUB_C_USER_READY: {
			return true;
		}
		}

		return true;
	}

	void SendGameMessage(int wSubCmdID, Object pData, int wDataSize, String data) {
		MSG_FOR_TABLE subcmd = new MSG_FOR_TABLE();
		subcmd.SetMsgType(Constants.PktID_DDZ_TableDataResult);
		subcmd.wSubCmd = wSubCmdID;
		subcmd.wChairID = Constants.INVALID_CHAIR;
		subcmd.strData = data;
		subcmd.byData = pData;
		String sData = subcmd.getJsonData();
		SendPacket(Constants.PktID_DDZ_TableDataResult, (MSG_PACKET) subcmd, 0, sData);
	}

	abstract boolean ProcessRecvdPacket(int dwMaxPacketSize);

	void Send(Object pMsg, int wSize) {
		OnRecv(pMsg, wSize);
	}

	boolean SendPacket(int wMsgType, MSG_PACKET pMsg, int wSize, Object data) {
		pMsg.SetMsgType(wMsgType);
		Send(pMsg, wSize);
		return true;
	}

	boolean OnRecv(Object pMsg, int wSize) {
		MSG_FOR_TABLE pTableCmd = (MSG_FOR_TABLE) pMsg;
		OnGameMessage(pTableCmd.wSubCmd, pMsg, wSize);
		return true;
	}

	void Update(int frameTime) {
		mTimerManager.Update(frameTime);
		ProcessRecvdPacket(4096);
	}

	int SetTimer(int nIDEvent, float nElapse) {
		return mTimerManager.SetTimer(nIDEvent, nElapse, false);
	}

	int SetTimer(int nIDEvent, float nElapse, boolean loop) {
		return mTimerManager.SetTimer(nIDEvent, nElapse, loop);
	}

	boolean KillTimer(int nIDEvent) {
		return mTimerManager.KillTimer(nIDEvent);
	}

	abstract void OnTimer(int nIDEvent);

	boolean SendSocketData(int wSubCmdID) {
		return SendData(wSubCmdID);
	}

	boolean SendSocketData(int wSubCmdID, Object pData) {
		return SendData(wSubCmdID, pData, 0, "");
	}

	boolean SendData(int wSubCmdID) {
		return SendData(wSubCmdID, 0, 0, "");
	}

	boolean SendData(int wSubCmdID, Object pData, int wDataSize, String data) {
		return mTable.SendGameMessage(wSubCmdID, pData, wDataSize, GetChairID(), data);
	}

	boolean SendUserReady(Object pData, int wDataSize) {
		return SendData(Constants.SUB_C_USER_READY, pData, wDataSize, "");
	}

	abstract public void OnGameBegin();

	abstract public void OnGameEnd();

	// �����˿�
	abstract boolean OnSubSendCard(Object pBuffer, int wDataSize);

	// �û��з�
	abstract boolean OnSubLandScore(Object pBuffer, int wDataSize);

	// ��Ϸ��ʼ
	abstract boolean OnSubGameStart(Object pBuffer, int wDataSize);

	// �û�����
	abstract boolean OnSubOutCard(Object pBuffer, int wDataSize);

	// ��������
	abstract boolean OnSubPassCard(Object pBuffer, int wDataSize);

	// ��Ϸ����
	abstract boolean OnSubGameEnd(Object pBuffer, int wDataSize);

	// ��Ϸ�й�
	abstract boolean OnSubTrustee(Object pBuffer, int wDataSize);

	// ������Ϣ
	abstract boolean OnEventGameScene(int cbGameStatus, boolean bLookonOther, Object pData, int wDataSize);

}
